/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import org.dom4j.Attribute;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentException;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.io.SAXReader;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class DataSourceConfig
/*     */ {
/*  32 */   private boolean syncAllFolders = true;
/*     */   private List<Service> services;
/*     */   private static final String SYNC_ALL_FOLDERS = "syncAllFolders";
/*     */   private static final String SERVICE = "service";
/*     */   
/*     */   public static DataSourceConfig read(File file)
/*     */     throws IOException
/*     */   {
/*  40 */     SAXReader reader = new SAXReader();
/*  41 */     reader.setIgnoreComments(true);
/*  42 */     FileInputStream is = new FileInputStream(file);
/*     */     try {
/*  44 */       Document doc = reader.read(is);
/*  45 */       return new DataSourceConfig().read(doc.getRootElement());
/*     */     } catch (DocumentException e) {
/*  47 */       throw invalidConfig(e.getMessage());
/*     */     } finally {
/*  49 */       is.close();
/*     */     }
/*     */   }
/*     */   
/*     */   private DataSourceConfig() {
/*  54 */     this.services = new ArrayList();
/*     */   }
/*     */   
/*     */   public boolean isSyncAllFolders() {
/*  58 */     return this.syncAllFolders;
/*     */   }
/*     */   
/*     */   public List<Service> getServices() {
/*  62 */     return this.services;
/*     */   }
/*     */   
/*     */   public Service getService(String name) {
/*  66 */     for (Service service : this.services) {
/*  67 */       if (name.equalsIgnoreCase(service.getName())) {
/*  68 */         return service;
/*     */       }
/*     */     }
/*  71 */     return null;
/*     */   }
/*     */   
/*     */   private DataSourceConfig read(Element element) throws IOException {
/*  75 */     for (Object obj : element.attributes()) {
/*  76 */       Attribute attr = (Attribute)obj;
/*  77 */       String name = attr.getName();
/*  78 */       if (name.equals("syncAllFolders")) {
/*  79 */         this.syncAllFolders = Boolean.valueOf(attr.getValue()).booleanValue();
/*     */       } else {
/*  81 */         throw invalidConfig("Unrecognized attribute name: " + name);
/*     */       }
/*     */     }
/*  84 */     for (Object obj : element.elements()) {
/*  85 */       Element el = (Element)obj;
/*  86 */       String name = el.getName();
/*  87 */       if (name.equals("service")) {
/*  88 */         this.services.add(new Service().read(el));
/*     */       } else {
/*  90 */         throw invalidConfig("Unrecognized element name: " + name);
/*     */       }
/*     */     }
/*  93 */     return this;
/*     */   }
/*     */   
/*     */   public static final class Service {
/*     */     private String name;
/*  98 */     private boolean saveToSent = true;
/*     */     private String calDavTargetUrl;
/*     */     private String calDavPrincipalPath;
/*     */     private List<DataSourceConfig.Folder> folders;
/*     */     private static final String NAME = "name";
/*     */     private static final String SAVE_TO_SENT = "saveToSent";
/*     */     private static final String CAL_DAV_TARGET_URL = "calDavTargetUrl";
/*     */     private static final String CAL_DAV_PRINCIPAL_PATH = "calDavPrincipalPath";
/*     */     private static final String FOLDER = "folder";
/*     */     
/*     */     public Service()
/*     */     {
/* 110 */       this.folders = new ArrayList();
/*     */     }
/*     */     
/*     */     public String getName() {
/* 114 */       return this.name;
/*     */     }
/*     */     
/*     */     public boolean isSaveToSent() {
/* 118 */       return this.saveToSent;
/*     */     }
/*     */     
/*     */     public String getCalDavTargetUrl() {
/* 122 */       return this.calDavTargetUrl;
/*     */     }
/*     */     
/*     */     public String getCalDavPrincipalPath() {
/* 126 */       return this.calDavPrincipalPath;
/*     */     }
/*     */     
/*     */     public List<DataSourceConfig.Folder> getFolders() {
/* 130 */       return this.folders;
/*     */     }
/*     */     
/*     */     public DataSourceConfig.Folder getFolderByLocalPath(String path) {
/* 134 */       for (DataSourceConfig.Folder folder : this.folders) {
/* 135 */         if (path.equalsIgnoreCase(folder.getLocalPath())) {
/* 136 */           return folder;
/*     */         }
/*     */       }
/* 139 */       return null;
/*     */     }
/*     */     
/*     */     public DataSourceConfig.Folder getFolderByRemotePath(String path) {
/* 143 */       for (DataSourceConfig.Folder folder : this.folders) {
/* 144 */         if (path.equalsIgnoreCase(folder.getRemotePath())) {
/* 145 */           return folder;
/*     */         }
/*     */       }
/* 148 */       return null;
/*     */     }
/*     */     
/*     */     private Service read(Element element) {
/* 152 */       for (Object obj : element.attributes()) {
/* 153 */         Attribute attr = (Attribute)obj;
/* 154 */         String name = attr.getName();
/* 155 */         if (name.equals("name")) {
/* 156 */           this.name = attr.getValue();
/* 157 */         } else if (name.equals("saveToSent")) {
/* 158 */           this.saveToSent = Boolean.valueOf(attr.getValue()).booleanValue();
/* 159 */         } else if (name.equals("calDavTargetUrl")) {
/* 160 */           this.calDavTargetUrl = attr.getValue();
/* 161 */         } else if (name.equals("calDavPrincipalPath")) {
/* 162 */           this.calDavPrincipalPath = attr.getValue();
/*     */         } else {
/* 164 */           throw DataSourceConfig.invalidConfig("Unrecognized service attribute name: " + name);
/*     */         }
/*     */       }
/* 167 */       if (this.name == null) {
/* 168 */         throw DataSourceConfig.invalidConfig("Missing service name attribute");
/*     */       }
/* 170 */       for (Object obj : element.elements()) {
/* 171 */         Element el = (Element)obj;
/* 172 */         String name = el.getName();
/* 173 */         if (name.equals("folder")) {
/* 174 */           this.folders.add(new DataSourceConfig.Folder().read(el));
/*     */         } else {
/* 176 */           throw DataSourceConfig.invalidConfig("Unrecognized service element name: " + name);
/*     */         }
/*     */       }
/* 179 */       return this;
/*     */     }
/*     */   }
/*     */   
/*     */   public static final class Folder
/*     */   {
/*     */     private String localPath;
/*     */     private String remotePath;
/*     */     private boolean ignore;
/*     */     private boolean sync;
/*     */     private static final String LOCAL_PATH = "localPath";
/*     */     private static final String REMOTE_PATH = "remotePath";
/*     */     private static final String IGNORE = "ignore";
/*     */     private static final String SYNC = "sync";
/*     */     
/*     */     public String getLocalPath() {
/* 195 */       return this.localPath;
/*     */     }
/*     */     
/*     */     public String getRemotePath() {
/* 199 */       return this.remotePath;
/*     */     }
/*     */     
/*     */     public boolean isIgnore() {
/* 203 */       return this.ignore;
/*     */     }
/*     */     
/*     */     public boolean isSync() {
/* 207 */       return this.sync;
/*     */     }
/*     */     
/*     */     private Folder read(Element element) {
/* 211 */       for (Object obj : element.attributes()) {
/* 212 */         Attribute attr = (Attribute)obj;
/* 213 */         String name = attr.getName();
/* 214 */         if (name.equals("remotePath")) {
/* 215 */           this.remotePath = attr.getValue();
/* 216 */         } else if (name.equals("localPath")) {
/* 217 */           this.localPath = attr.getValue();
/* 218 */         } else if (name.equals("ignore")) {
/* 219 */           this.ignore = Boolean.valueOf(attr.getValue()).booleanValue();
/* 220 */         } else if (name.equals("sync")) {
/* 221 */           this.sync = Boolean.valueOf(attr.getValue()).booleanValue();
/*     */         } else {
/* 223 */           throw DataSourceConfig.invalidConfig("Unrecognized folder attribute name: " + name);
/*     */         }
/*     */       }
/* 226 */       if (!element.elements().isEmpty()) {
/* 227 */         throw DataSourceConfig.invalidConfig("Folder should not contain any sub-elements");
/*     */       }
/* 229 */       if (this.remotePath == null) {
/* 230 */         throw DataSourceConfig.invalidConfig("Missing folder remotePath attribute");
/*     */       }
/* 232 */       if (this.localPath == null) {
/* 233 */         this.localPath = this.remotePath;
/*     */       }
/* 235 */       if (!this.localPath.startsWith("/")) {
/* 236 */         this.localPath = ("/" + this.localPath);
/*     */       }
/* 238 */       return this;
/*     */     }
/*     */   }
/*     */   
/*     */   private static IllegalArgumentException invalidConfig(String msg) {
/* 243 */     return new IllegalArgumentException("Invalid configuration: " + msg);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 247 */     DataSourceConfig config = read(new File(args[0]));
/* 248 */     System.out.println("config = " + config);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/DataSourceConfig.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */